package com.cy.store.controller;
import com.cy.store.dto.UserDTO;
import com.cy.store.entity.User;
import com.cy.store.service.UserService;
import com.cy.store.util.JsonResult;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.cy.store.controller.session.UserSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

import static com.cy.store.constant.Constant.SUCCESS_MESSAGE;

@RestController
@Slf4j
@RequestMapping("/users")
//@Api(tags = "用户zcdl相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    /***注册接口
     *
     * @param userDTO
     * @return
     */
    @PostMapping ("/reg")
   // @ApiOperation("用户注册接口")
    public JsonResult register(UserDTO userDTO) {
        log.info("用户注册信息:{}", userDTO);
        userService.reg(userDTO);
        return new JsonResult(SUCCESS_MESSAGE);
    }

    /***登录接口
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/login")
   // @ApiOperation("用户登录接口")
    public JsonResult<User> login(UserDTO userDTO, HttpSession httpSession){
        log.info("用户登录信息为:{}",userDTO);
        User date=userService.login(userDTO);

        httpSession.setAttribute("uid",date.getUid());
        httpSession.setAttribute("username",date.getUsername());

        log.info("session中的id数据:{}",UserSession.getUIdBySession(httpSession));
        log.info("session中的username数据:{}",UserSession.getUsernameBySession(httpSession));

        return new JsonResult<User>(SUCCESS_MESSAGE,date);

    }
}


